﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace HANS_Base.Model
{
    public  class JsonResultData
    {
        public JsonResultData()
        {
            Messages = new List<string>();
        }

        public bool Success { get; set; }

        public string RedirectUrl { get; set; }

        public bool ReloadPage { get; set; }

        public object Model { get; set; }

        public int Code { get; set; }

        public List<string> Messages { get; set; }

        public void AddMessage(string message)
        {
            Messages.Add(message);
        }

        public void AddSuccessMessage(string message)
        {
            Success = true;
            Messages.Add(message);
        }

        public void AddModelValidErrorMessage(ModelStateDictionary dictionary)
        {
            Success = false;
            foreach (var kv in dictionary)
            {
                foreach (var err in kv.Value.Errors)
                {
                    AddMessage(err.ErrorMessage);
                }
            }
        }

        /// <summary>
        /// 去除不需要的验证
        /// </summary>
        /// <param name="prefix">验证的前缀</param>
        public void RetainsModelValidNotNeedKey(ModelStateDictionary dictionary, string prefix)
        {
            foreach (var key in dictionary.ToList())
            {
                if (!key.Key.StartsWith(prefix))
                {
                    dictionary.Remove(key.Key);
                }
            }
        }
    }
}
